package com.example.demo.tool.netty.chat;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 自定义编码
 */
public class MyMessageEncoder extends MessageToByteEncoder<MyMessageProtocol> {

    /**
     * 重写编码方法
     * @param channelHandlerContext
     * @param myMessageProtocol
     * @param byteBuf
     * @throws Exception
     */
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, MyMessageProtocol myMessageProtocol, ByteBuf byteBuf) throws Exception {
        byteBuf.writeInt(myMessageProtocol.getLen());
        byteBuf.writeBytes(myMessageProtocol.getContent());
    }
}
